home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / python-rdflib / rdflib / sparql / Query.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  40.2 KB  |  899 lines

  1. import types, sets
  2.  
  3. from rdflib import URIRef, BNode, Literal
  4. from rdflib.Identifier import Identifier
  5.  
  6. from rdflib.util import check_subject, list2set
  7.  
  8. from rdflib.sparql import SPARQLError
  9. from rdflib.sparql.Unbound import Unbound
  10. from rdflib.sparql.sparqlGraph import SPARQLGraph
  11. from rdflib.sparql.graphPattern import GraphPattern
  12.  
  13. class SessionBNode(BNode):
  14.     """
  15.     Special 'session' BNodes.  I.e., BNodes at the query side which refer to 
  16.     BNodes in persistence
  17.     """
  18.     pass        
  19.  
  20. def _checkOptionals(pattern,optionals) :
  21.     """
  22.     The following remark in the SPARQL document is important:
  23.  
  24.     'If a new variable is mentioned in an optional block (as mbox and
  25.     hpage are mentioned in the previous example), that variable can be
  26.     mentioned in that block and can not be mentioned in a subsequent
  27.     block.'
  28.  
  29.     What this means is that the various optional blocks do not
  30.     interefere at this level and there is no need for a check whether
  31.     a binding in a subsequent block clashes with an earlier optional
  32.     block.
  33.  
  34.     This method checks whether this requirement is fulfilled. Raises a
  35.     SPARQLError exception if it is not (the rest of the algorithm
  36.     relies on this, so checking it is a good idea...)
  37.  
  38.     @param pattern: graph pattern
  39.     @type pattern: L{GraphPattern<rdflib.sparql.GraphPattern>}
  40.     @param optionals: graph pattern
  41.     @type optionals: L{GraphPattern<rdflib.sparql.GraphPattern>}
  42.     @raise SPARQLError: if the requirement is not fulfilled
  43.     """
  44.     for i in xrange(0,len(optionals)) :
  45.         for c in optionals[i].unbounds :
  46.             if c in pattern.unbounds :
  47.                 # this is fine, an optional query variable can appear in the main pattern, too
  48.                 continue
  49.             if i > 0 :
  50.                 for j in xrange(0,i) :
  51.                     if c in optionals[j].unbounds :
  52.                         # This means that:
  53.                         #   - the variable is not in the main pattern (because the previous if would have taken care of it)
  54.                         #   - the variable is in the previous optional: ie, Error!
  55.                         raise SPARQLError("%s is an illegal query string, it appear in a previous OPTIONAL clause" % c)
  56.  
  57.  
  58. def _variablesToArray(variables,name='') :
  59.     """Turn an array of Variables or query strings into an array of query strings. If the 'variables'
  60.     is in fact a single string or Variable, then it is also put into an array.
  61.  
  62.     @param variables: a string, a unicode, or a Variable, or an array of those (can be mixed, actually). As a special case,
  63.     if the value is "*", it returns None (this corresponds to the wildcard in SPARQL)
  64.     @param name: the string to be used in the error message
  65.     """
  66.     if isinstance(variables,basestring) :
  67.         if variables == "*" :
  68.             return None
  69.         else :
  70.             return [variables]
  71.     elif isinstance(variables,Unbound) :
  72.         return [variables.name]
  73.     elif type(variables) == list or type(variables) == tuple :
  74.         retval = []
  75.         for s in variables :
  76.             if isinstance(s,basestring) :
  77.                 retval.append(s)
  78.             elif isinstance(s,Unbound) :
  79.                 retval.append(s.name)
  80.             else :
  81.                 raise SPARQLError("illegal type in '%s'; must be a string, unicode, or a Variable" % name)
  82.     else :
  83.         raise SPARQLError("'%s' argument must be a string, a Variable, or a list of those" % name)
  84.     return retval
  85.  
  86. def _createInitialBindings(pattern) :
  87.     """Creates an initial binding directory for the Graph Pattern by putting a None as a value for each
  88.     query variable.
  89.  
  90.     @param pattern: graph pattern
  91.     @type pattern: L{GraphPattern<rdflib.sparql.GraphPattern>}
  92.     """
  93.     bindings = {}
  94.     for c in pattern.unbounds :
  95.         bindings[c] = None
  96.     return bindings
  97.  
  98.  
  99. class _SPARQLNode:
  100.     """
  101.     The SPARQL implementation is based on the creation of a tree, each
  102.     level for each statement in the 'where' clause of SPARQL.
  103.  
  104.     Each node maintains a 'binding' dictionary, with the variable
  105.     names and either a None if not yet bound, or the binding
  106.     itself. The method 'expand' tries to make one more step of binding
  107.     by looking at the next statement: it takes the statement of the
  108.     current node, binds the variables if there is already a binding,
  109.     and looks at the triple store for the possibilities. If it finds
  110.     valid new triplets, that will bind some more variables, and
  111.     children will be created with the next statement in the 'where'
  112.     array with a new level of bindings. This is done for each triplet
  113.     found in the store, thereby branching off the tree. If all
  114.     variables are already bound but the statement, with the bound
  115.     variables, is not 'true' (ie, there is no such triple in the
  116.     store), the node is marked as 'clash' and no more expansion is
  117.     made; this node will then be thrown away by the parent. If I{all}
  118.     children of a node is a clash, then it is marked as a clash
  119.     itself.
  120.  
  121.     At the end of the process, the leaves of the tree are searched; if
  122.     a leaf is such that:
  123.  
  124.       - all variables are bound
  125.       - there is no clash
  126.  
  127.     then the bindings are returned as possible answers to the query.
  128.  
  129.     The optional clauses are treated separately: each 'valid' leaf is
  130.     assigned an array of expansion trees that contain the optional
  131.     clauses (that may have some unbound variables bound at the leaf,
  132.     though).
  133.  
  134.     @ivar parent: parent in the tree
  135.     @type parent: _SPARQLNode
  136.     @ivar children: the children (in an array)
  137.     @type children: array of _SPARQLNode
  138.     @ivar bindings:  copy of the bindings locally
  139.     @type bindings: dictionary
  140.     @ivar statement:  the current statement
  141.     @type statement: a (s,p,o,f) tuple ('f' is the local filter or None)
  142.     @ivar rest:  the rest of the statements (an array)
  143.     @ivar clash: intialized to False
  144.     @type clash: Boolean
  145.     @ivar bound:  True or False depending on whether all variables are bound in self.binding
  146.     @type bound: Boolean
  147.     @ivar optionalTrees: expansion trees for optional statements
  148.     @type optionalTrees: array of _SPARQLNode instances
  149.     """
  150.     def __init__(self,parent,bindings,statements,tripleStore) :
  151.         """
  152.         @param parent:     parent node
  153.         @param bindings:   a dictionary with the bindings that are already done or with None value if no binding yet
  154.         @param statements: array of statements from the 'where' clause. The first element is
  155.         for the current node, the rest for the children. If empty, then no
  156.         expansion occurs (ie, the node is a leaf)
  157.         @param tripleStore: the 'owner' triple store
  158.         @type tripleStore: L{sparqlGraph<rdflib.sparql.sparqlGraph.sparqlGraph>}
  159.         """
  160.         self.tripleStore         = tripleStore
  161.         self.bindings            = bindings
  162.         self.optionalTrees       = []
  163.         if None in bindings.values() :
  164.             self.bound = False
  165.         else :
  166.             self.bound = True
  167.         self.clash     = False
  168.  
  169.         self.parent    = parent
  170.         self.children  = []
  171.  
  172.         if len(statements) > 0 :
  173.             self.statement = statements[0]
  174.             self.rest      = statements[1:]
  175.         else :
  176.             self.statement = None
  177.             self.rest      = None
  178.  
  179.     def returnResult(self,select) :
  180.         """
  181.         Collect the result by search the leaves of the the tree. The
  182.         variables in the select are exchanged against their bound
  183.         equivalent (if applicable). This action is done on the valid
  184.         leaf nodes only, the intermediate nodes only gather the
  185.         children's results and combine it in one array.
  186.  
  187.         @param select: the array of unbound variables in the original
  188.         select that do not appear in any of the optionals. If None,
  189.         the full binding should be considered (this is the case for
  190.         the SELECT * feature of SPARQL)
  191.         @returns: an array of dictionaries with non-None bindings.
  192.         """
  193.         if len(self.children) > 0 :
  194.             # combine all the results of all the kids into one array
  195.             retval = []
  196.             for c in self.children :
  197.                 res = c.returnResult(select)
  198.                 # res is a list of dictionaries, so each tuple should be taken out and added to the result
  199.                 for t in res :
  200.                     retval.append(t)
  201.             return retval
  202.         else :
  203.             retval = []
  204.             if self.bound == True and self.clash == False :
  205.                 # This node should be able to contribute to the final results:
  206.                 result = {}
  207.                 # This where the essential happens: the binding values are used to construct the selection result
  208.                 if select :
  209.                     for a in select :
  210.                         if a in self.bindings :
  211.                             result[a] = self.bindings[a]
  212.                 else :
  213.                     result = self.bindings.copy()
  214.                 # Initial return block. If there is no optional processing, that is the result, in fact,
  215.                 # because the for cycle below will not happen
  216.                 retval = [result]
  217.                 # The following remark in the SPARQL document is important at this point:
  218.                 # "If a new variable is mentioned in an optional block (as mbox and hpage are mentioned
  219.                 #  in the previous example), that variable can be mentioned in that block and can not be
  220.                 #  mentioned in a subsequent block."
  221.                 # What this means is that the various optional blocks do not interefere at this point
  222.                 # and there is no need for a check whether a binding in a subsequent block
  223.                 # clashes with an earlier optional block.
  224.                 # The API checks this at the start.
  225.                 # What happens here is that the result of the optional expantion is added to what is already
  226.                 # there. Note that this may lead to a duplication of the result so far, if there are several
  227.                 # alternatives returned by the optionals!
  228.                 for optTree in self.optionalTrees :
  229.                     # get the results from the optional Tree...
  230.                     optionals = optTree.returnResult(select)
  231.                     # ... and extend the results accumulated so far with the new bindings
  232.                     # It is worth separating the case when there is only one optional block; it avoids
  233.                     # unnecessary copying
  234.                     if len(optionals) == 0 :
  235.                         # no contribution at all :-(
  236.                         continue
  237.                     elif len(optionals) == 1 :
  238.                         optResult = optionals[0]
  239.                         for res in retval :
  240.                             for k in optResult :
  241.                                 if optResult[k] != None :
  242.                                     res[k] = optResult[k]
  243.                     else :
  244.                         newRetval = []
  245.                         for optResult in optionals :
  246.                             # Each binding dictionary we have so far should be copied with the new values
  247.                             for res in retval :
  248.                                 dct = {}
  249.                                 # copy the content of the exisiting bindings ...
  250.                                 dct = res.copy()
  251.                                 # ... and extend it with the optional results
  252.                                 for k in optResult :
  253.                                     if optResult[k] != None :
  254.                                         dct[k] = optResult[k]
  255.                                 newRetval.append(dct)
  256.                         retval = newRetval
  257.             return retval
  258.  
  259.  
  260.     def expandSubgraph(self,subTriples,pattern) :
  261.         """
  262.         Method used to collect the results. There are two ways to
  263.         invoke the method:
  264.  
  265.           - if the pattern argument is not None, then this means the
  266.           construction of a separate triple store with the
  267.           results. This means taking the bindings in the node, and
  268.           constuct the graph via the
  269.           L{construct<rdflib.sparql.graphPattern.GraphPattern.construct>}
  270.           method. This happens on the valid leafs; intermediate nodes
  271.           call the same method recursively - otherwise, a leaf returns
  272.           an array of the bindings, and intermediate methods aggregate
  273.           those.
  274.  
  275.         In both cases, leaf nodes may successifely expand the optional
  276.         trees that they may have.
  277.  
  278.         @param subTriples: the triples so far
  279.         @type subTriples: L{sparqlGraph<rdflib.sparql.sparqlGraph.sparqlGraph>}
  280.         @param pattern: a graph pattern used to construct a graph
  281.         @type pattern: L{GraphPattern<rdflib.sparql.graphPattern.GraphPattern>}
  282.         @return: if pattern is not None, an array of binding dictionaries
  283.         """
  284.         def b(r,bind) :
  285.             if type(r) == str :
  286.                 val = bind[r]
  287.                 if val == None :
  288.                     raise RuntimeError()
  289.                 return bind[r]
  290.             else :
  291.                 return r
  292.         if len(self.children) > 0 :
  293.             # all children return an array of bindings (each element being a dictionary)
  294.             if pattern == None :
  295.                 retval = reduce(lambda x,y: x+y, [x.expandSubgraph(subTriples,None) for x in self.children],[])
  296.                 (s,p,o,func) = self.statement
  297.                 for bind in retval :
  298.                     try :
  299.                         st = (b(s,bind),b(p,bind),b(o,bind))
  300.                         subTriples.add(st)
  301.                     except :
  302.                         # any exception means a None value creeping in, or something similar..
  303.                         pass
  304.                 return retval
  305.             else :
  306.                 for x in self.children :
  307.                     x.expandSubgraph(subTriples,pattern)
  308.         else :
  309.             # return the local bindings if any. Not the optional trees should be added, too!
  310.             if self.bound == True and self.clash == False :
  311.                 # Get the possible optional branches:
  312.                 for t in self.optionalTrees :
  313.                     t.expandSubgraph(subTriples,pattern)
  314.                 if pattern == None :
  315.                     return [self.bindings]
  316.                 else :
  317.                     pattern.construct(subTriples,self.bindings)
  318.             else :
  319.                 return []
  320.  
  321.  
  322.     def _bind(self,r) :
  323.         """
  324.         @param r: string
  325.         @return: returns None if no bindings occured yet, the binding otherwise
  326.         """
  327.         if isinstance(r,basestring) and not isinstance(r,Identifier)  :
  328.             if self.bindings[r] == None :
  329.                 return None
  330.             else :
  331.                 return self.bindings[r]
  332.         elif isinstance(r,(SessionBNode)):
  333.             return r            
  334.         elif isinstance(r,(BNode)):
  335.             return self.bindings.get(r)            
  336.         else :
  337.             return r
  338.  
  339.     def expand(self,constraints) :
  340.         """
  341.         The expansion itself. See class comments for details.
  342.  
  343.         @param constraints: array of global constraining (filter) methods
  344.         """
  345.         # if there are no more statements, that means that the constraints have been fully expanded
  346.         if self.statement :
  347.             # decompose the statement into subject, predicate and object
  348.             # default setting for the search statement
  349.             # see if subject (resp. predicate and object) is already bound. This
  350.             # is done by taking over the content of self.dict if not None and replacing
  351.             # the subject with that binding
  352.             # the (search_subject,search_predicate,search_object) is then created
  353.             (s,p,o,func) = self.statement
  354.             # put the bindings we have so far into the statement; this may add None values,
  355.             # but that is exactly what RDFLib uses in its own search methods!
  356.             (search_s,search_p,search_o) = (self._bind(s),self._bind(p),self._bind(o))
  357.             if self.tripleStore.graphVariable:
  358.                 assert hasattr(self.tripleStore.graph,'quads'),\
  359.                   "Graph graph patterns can only be used with Graph instances with a quad method"
  360.                 
  361.                 searchRT = self.tripleStore.graph.quads((search_s,search_p,search_o))
  362.             else:
  363.                 searchRT = self.tripleStore.graph.triples((search_s,search_p,search_o))
  364.             for tripleOrQuad in searchRT:
  365.             #for (result_s,result_p,result_o) in self.tripleStore.graph.triples((search_s,search_p,search_o)) :
  366.                 if self.tripleStore.graphVariable:
  367.                     (result_s,result_p,result_o,parentGraph) = tripleOrQuad
  368.                 else:
  369.                     (result_s,result_p,result_o) = tripleOrQuad
  370.                 # if a user defined constraint has been added, it should be checked now
  371.                 if func != None and func(result_s,result_p,result_o) == False :
  372.                     # Oops, this result is not acceptable, jump over it!
  373.                     continue
  374.                 # create a copy of the current bindings, by also adding the new ones from result of the search
  375.                 new_bindings = self.bindings.copy()
  376.                 if search_s == None : new_bindings[s] = result_s
  377.                 if search_p == None : new_bindings[p] = result_p
  378.                 if search_o == None : new_bindings[o] = result_o
  379.                 if self.tripleStore.graphVariable:
  380.                     new_bindings[self.tripleStore.graphVariable] = parentGraph.identifier
  381.  
  382.                 # Recursion starts here: create and expand a new child
  383.                 child = _SPARQLNode(self,new_bindings,self.rest,self.tripleStore)
  384.                 child.expand(constraints)                
  385.                 # if the child is a clash then no use adding it to the tree, it can be forgotten
  386.                 if self.clash == False :
  387.                     self.children.append(child)
  388.  
  389.             if len(self.children) == 0 :
  390.                 # this means that the constraints could not be met at all with this binding!!!!
  391.                 self.clash = True
  392.         else :
  393.             # this is if all bindings are done; the conditions (ie, global constraints) are still to be checked
  394.             if self.bound == True and self.clash == False :
  395.                 for func in constraints :
  396.                     if func(self.bindings) == False :
  397.                         self.clash = True
  398.                         break
  399.  
  400.     def expandOptions(self,bindings,statements,constraints) :
  401.         """
  402.         Managing optional statements. These affect leaf nodes only, if
  403.         they contain 'real' results. A separate Expansion tree is
  404.         appended to such a node, one for each optional call.
  405.  
  406.         @param bindings: current bindings dictionary
  407.  
  408.         @param statements: array of statements from the 'where'
  409.         clause. The first element is for the current node, the rest
  410.         for the children. If empty, then no expansion occurs (ie, the
  411.         node is a leaf). The bindings at this node are taken into
  412.         account (replacing the unbound variables with the real
  413.         resources) before expansion
  414.  
  415.         @param constraints: array of constraint (filter) methods
  416.         """
  417.         def replace(key,resource,tupl) :
  418.             s,p,o,func = tupl
  419.             if key == s : s = resource
  420.             if key == p : p = resource
  421.             if key == o : o = resource
  422.             return (s,p,o,func)
  423.  
  424.         if len(self.children) == 0  :
  425.             # this is a leaf in the original expansion
  426.             if self.bound == True and self.clash == False :
  427.                 # see if the optional bindings can be reduced because they are already
  428.                 # bound by this node
  429.                 toldBNodeLookup = {}
  430.                 for key in self.bindings :
  431.                     normalizedStatements = []
  432.                     for t in statements:
  433.                         val = self.bindings[key]
  434.                         if isinstance(val,BNode) and val not in toldBNodeLookup:
  435.                             toldBNodeLookup[val] = val
  436.                         normalizedStatements.append(replace(key,self.bindings[key],t))
  437.                     statements = normalizedStatements
  438.                     if key in bindings :
  439.                         del bindings[key]
  440.                 bindings.update(toldBNodeLookup)
  441.                 optTree = _SPARQLNode(None,bindings,statements,self.tripleStore)
  442.                 self.optionalTrees.append(optTree)
  443.                 optTree.expand(constraints)
  444.         else :
  445.             for c in self.children :
  446.                 c.expandOptions(bindings,statements,constraints)
  447.  
  448.  
  449. def _processResults(select,arr) :
  450.     '''
  451.     The result in an expansion node is in the form of an array of
  452.     binding dictionaries.  The caller should receive an array of
  453.     tuples, each tuple representing the final binding (or None) I{in
  454.     the order of the original select}. This method is the last step of
  455.     processing by processing these values to produce the right result.
  456.  
  457.     @param select: the original selection list. If None, then the
  458.     binding should be taken as a whole (this corresponds to the SELECT * feature of SPARQL)
  459.     @param arr: the array of bindings
  460.     @type arr:
  461.     an array of dictionaries
  462.     @return: a list of tuples with the selection results
  463.     '''
  464.     retval = []
  465.     if select :
  466.         for bind in arr :
  467.             # each result binding must be taken separately
  468.             qresult = []
  469.             for s in select :
  470.                 if s in bind :
  471.                     qresult.append(bind[s])
  472.                 else :
  473.                     qresult.append(None)
  474.             # as a courtesy to the user, if the selection has one single element only, than we do no
  475.             # put in a tuple, just add it that way:
  476.             if len(select) == 1 :
  477.                 retval.append(qresult[0])
  478.             else :
  479.                 retval.append(tuple(qresult))
  480.     else :
  481.         # this is the case corresponding to a SELECT * query call
  482.         for bind in arr:
  483.             qresult = [val for key,val in bind.items()]
  484.             if len(qresult) == 1 :
  485.                 retval.append(qresult[0])
  486.             else :
  487.                 retval.append(tuple(qresult))
  488.     return retval
  489.  
  490.  
  491. def query(graph, selection, patterns, optionalPatterns=[], initialBindings = {}) :
  492.     """
  493.     A shorthand for the creation of a L{Query} instance, returning
  494.     the result of a L{Query.select} right away. Good for most of
  495.     the usage, when no more action (clustering, etc) is required.
  496.  
  497.     @param selection: a list or tuple with the selection criteria,
  498.     or a single string. Each entry is a string that begins with a"?".
  499.  
  500.     @param patterns: either a
  501.     L{GraphPattern<rdflib.sparql.graphPattern.GraphPattern>}
  502.     instance or a list of instances thereof. Each pattern in the
  503.     list represent an 'OR' (or 'UNION') branch in SPARQL.
  504.  
  505.     @param optionalPatterns: either a
  506.     L{GraphPattern<rdflib.sparql.graphPattern.GraphPattern>}
  507.     instance or a list of instances thereof. For each elements in
  508.     the 'patterns' parameter is combined with each of the optional
  509.     patterns and the results are concatenated. The list may be
  510.     empty.
  511.  
  512.     @return: list of query results
  513.     @rtype: list of tuples
  514.     """
  515.     result = queryObject(graph, patterns,optionalPatterns,initialBindings)
  516.     if result == None :
  517.         # generate some proper output for the exception :-)
  518.         msg = "Errors in the patterns, no valid query object generated; "
  519.         if isinstance(patterns,GraphPattern) :
  520.             msg += ("pattern:\n%s" % patterns)
  521.         else :
  522.             msg += ("pattern:\n%s\netc..." % patterns[0])
  523.         raise SPARQLError(msg)
  524.     return result.select(selection)
  525.  
  526. def queryObject(graph, patterns, optionalPatterns=[], initialBindings = None) :
  527.     """
  528.     Creation of a L{Query} instance.
  529.  
  530.     @param patterns: either a
  531.     L{GraphPattern<rdflib.sparql.graphPattern.GraphPattern>}
  532.     instance or a list of instances thereof. Each pattern in the
  533.     list represent an 'OR' (or 'UNION') branch in SPARQL.
  534.  
  535.     @param optionalPatterns: either a
  536.     L{GraphPattern<rdflib.sparql.graphPattern.GraphPattern>}
  537.     instance or a list of instances thereof. For each elements in
  538.     the 'patterns' parameter is combined with each of the optional
  539.     patterns and the results are concatenated. The list may be
  540.     empty.
  541.  
  542.     @return: Query object
  543.     @rtype: L{Query}
  544.     """
  545.     def checkArg(arg,error) :
  546.         if arg == None :
  547.             return []
  548.         elif isinstance(arg,GraphPattern) :
  549.             return [arg]
  550.         elif type(arg) == list or type(arg) == tuple :
  551.             for p in arg :
  552.                 if not isinstance(p,GraphPattern) :
  553.                     raise SPARQLError("'%s' argument must be a GraphPattern or a list of those" % error)
  554.             return arg
  555.         else :
  556.             raise SPARQLError("'%s' argument must be a GraphPattern or a list of those" % error)
  557.  
  558.     finalPatterns         = checkArg(patterns,"patterns")
  559.     finalOptionalPatterns = checkArg(optionalPatterns,"optionalPatterns")
  560.  
  561.     retval = None
  562.     if not initialBindings:
  563.         initialBinding = {}
  564.     for pattern in finalPatterns :
  565.         # Check whether the query strings in the optional clauses are fine. If a problem occurs,
  566.         # an exception is raised by the function
  567.         _checkOptionals(pattern,finalOptionalPatterns)
  568.         bindings = _createInitialBindings(pattern)
  569.         if initialBindings:
  570.             bindings.update(initialBindings)
  571.         # This is the crucial point: the creation of the expansion tree and the expansion. That
  572.         # is where the real meal is, we had only an apetizer until now :-)
  573.         top = _SPARQLNode(None,bindings,pattern.patterns, graph)
  574.         top.expand(pattern.constraints)
  575.         for opt in finalOptionalPatterns :
  576.             bindings = _createInitialBindings(opt)
  577.             if initialBindings:
  578.                 bindings.update(initialBindings)
  579.             top.expandOptions(bindings,opt.patterns,opt.constraints)
  580.         r = Query(top, graph)
  581.         if retval == None :
  582.             retval = r
  583.         else :
  584.             # This branch is, effectively, the UNION clause of the draft
  585.             retval = retval + r
  586.     return retval
  587.  
  588.  
  589. class Query :
  590.     """
  591.     Result of a SPARQL query. It stores to the top of the query tree, and allows some subsequent
  592.     inquiries on the expanded tree. B{This class should not be
  593.     instantiated by the user,} it is done by the L{queryObject<SPARQL.queryObject>} method.
  594.  
  595.     """
  596.     def __init__(self,sparqlnode,triples,parent1=None,parent2=None) :
  597.         """
  598.         @param sparqlnode: top of the expansion tree
  599.         @type sparqlnode: _SPARQLNode
  600.         @param triples: triple store
  601.         @type triples: L{sparqlGraph<rdflib.sparql.sparqlGraph>}
  602.         @param parent1: possible parent Query when queries are combined by summing them up
  603.         @type parent1: L{Query}
  604.         @param parent2: possible parent Query when queries are combined by summing them up
  605.         @type parent2: L{Query}
  606.         """
  607.         self.top             = sparqlnode
  608.         self.triples         = triples
  609.         # if this node is the result of a sum...
  610.         self.parent1         = parent1
  611.         self.parent2         = parent2
  612.  
  613.     def __add__(self,other) :
  614.         """This may be useful when several queries are performed and
  615.         one wants the 'union' of those.  Caveat: the triple store must
  616.         be the same for each argument. This method is used internally
  617.         only anyway...  Efficiency trick (I hope it works): the
  618.         various additions on subgraphs are not done here; the results
  619.         are calculated only if really necessary, ie, in a lazy
  620.         evaluation manner.  This is achieved by storing self and the
  621.         'other' in the new object
  622.         """
  623.         return Query(None,self.triples,self,other)
  624.  
  625.     def _getFullBinding(self) :
  626.         """Retrieve the full binding, ie, an array of binding dictionaries
  627.         """
  628.         if self.parent1 != None and self.parent2 != None :
  629.             return self.parent1._getFullBinding() + self.parent2._getFullBinding()
  630.         else :
  631.             # remember: returnResult returns an array of dictionaries
  632.             return self.top.returnResult(None)
  633.  
  634.     def _getAllVariables(self):
  635.        """Retrieve the list of all variables, to be returned"""
  636.        if self.parent1 and self.parent2:
  637.            return list2set(self.parent1._getAllVariables() + self.parent2._getAllVariables())
  638.        else:
  639.            return list2set(self.top.bindings.keys())
  640.  
  641.     def _orderedSelect(self,selection,orderedBy,orderDirection) :
  642.         """
  643.         The variant of the selection (as below) that also includes the sorting. Because that is much less efficient, this is
  644.         separated into a distinct method that is called only if necessary. It is called from the L{select<select>} method.
  645.         
  646.         Because order can be made on variables that are not part of the final selection, this method retrieves a I{full}
  647.         binding from the result to be able to order it (whereas the core L{select<select>} method retrieves from the result
  648.         the selected bindings only). The full binding is an array of (binding) dictionaries; the sorting sorts this array
  649.         by comparing the bound variables in the respective dictionaries. Once this is done, the final selection is done.
  650.  
  651.         @param selection: Either a single query string, or an array or tuple thereof.
  652.         @param orderBy: either a function or a list of strings (corresponding to variables in the query). If None, no sorting occurs
  653.         on the results. If the parameter is a function, it must take two dictionary arguments (the binding dictionaries), return
  654.         -1, 0, and 1, corresponding to smaller, equal, and greater, respectively.
  655.         @param orderDirection: if not None, then an array of integers of the same length as orderBy, with values the constants
  656.         ASC or DESC (defined in the module). If None, an ascending order is used.
  657.         @return: selection results
  658.         @rtype: list of tuples
  659.         @raise SPARQLError: invalid sorting arguments
  660.         """
  661.         fullBinding = self._getFullBinding()
  662.         if type(orderedBy) is types.FunctionType :
  663.             _sortBinding = orderedBy
  664.         else :
  665.             orderKeys = _variablesToArray(orderedBy,"orderBy")
  666.             # see the direction
  667.             oDir = None # this is just to fool the interpreter's error message
  668.             if orderDirection is None :
  669.                 oDir = [ True for i in xrange(0,len(orderKeys)) ]
  670.             elif type(orderDirection) is types.BooleanType :
  671.                 oDir = [ orderDirection ]
  672.             elif type(orderDirection) is not types.ListType and type(orderDirection) is not types.TupleType :
  673.                 raise SPARQLError("'orderDirection' argument must be a list")
  674.             elif len(orderDirection) != len(orderKeys) :
  675.                 raise SPARQLError("'orderDirection' must be of an equal length to 'orderBy'")
  676.             else :
  677.                 oDir = orderDirection
  678.             def _sortBinding(b1,b2) :
  679.                 """The sorting method used by the array sort, with return values as required by the python run-time
  680.                 The to-be-compared data are dictionaries of bindings
  681.                 """
  682.                 for i in xrange(0,len(orderKeys)) :
  683.                     # each key has to be compared separately. If there is a clear comparison result on that key
  684.                     # then we are done, but when that is not the case, the next in line should be used
  685.                     key       = orderKeys[i]
  686.                     direction = oDir[i]
  687.                     if key in b1 and key in b2 :
  688.                         val1 = b1[key]
  689.                         val2 = b2[key]
  690.                         if val1 != None and val2 != None :
  691.                             if direction :
  692.                                 if   val1 < val2 : return -1
  693.                                 elif val1 > val2 : return 1
  694.                             else :
  695.                                 if   val1 > val2 : return -1
  696.                                 elif val1 < val2 : return 1
  697.                 return 0
  698.         # get the full Binding sorted
  699.         fullBinding.sort(_sortBinding)
  700.         # remember: _processResult turns the expansion results (an array of dictionaries)
  701.         # into an array of tuples in the right, original order
  702.         retval = _processResults(selection,fullBinding)
  703.         return retval
  704.  
  705.     def select(self,selection,distinct=True,limit=None,orderBy=None,orderAscend=None,offset=0) :
  706.         """
  707.         Run a selection on the query.
  708.  
  709.         @param selection: Either a single query string, or an array or tuple thereof.
  710.         @param distinct: if True, identical results are filtered out
  711.         @type distinct: Boolean
  712.         @param limit: if set to an integer value, the first 'limit' number of results are returned; all of them otherwise
  713.         @type limit: non negative integer
  714.         @param orderBy: either a function or a list of strings (corresponding to variables in the query). If None, no sorting occurs
  715.         on the results. If the parameter is a function, it must take two dictionary arguments (the binding dictionaries), return
  716.         -1, 0, and 1, corresponding to smaller, equal, and greater, respectively.
  717.         @param orderAscend: if not None, then an array of booelans of the same length as orderBy, True for ascending and False
  718.         for descending. If None, an ascending order is used.
  719.         @offset the starting point of return values in the array of results. Obviously, this parameter makes real sense if
  720.         some sort of order is defined.
  721.         @return: selection results
  722.         @rtype: list of tuples
  723.         @raise SPARQLError: invalid selection argument
  724.         """
  725.         def _uniquefyList(lst) :
  726.             """Return a copy of the list but possible duplicate elements are taken out. Used to
  727.             post-process the outcome of the query
  728.             @param lst: input list
  729.             @return: result list
  730.             """
  731.             if len(lst) <= 1 :
  732.                 return lst
  733.             else :
  734.                 # must be careful! Using the quick method of Sets destroy the order. Ie, if this was ordered, then
  735.                 # a slower but more secure method should be used
  736.                 if orderBy != None :
  737.                     retval = []
  738.                     for i in xrange(0,len(lst)) :
  739.                         v = lst[i]
  740.                         skip = False
  741.                         for w in retval :
  742.                             if w == v :
  743.                                 skip = True
  744.                                 break
  745.                         if not skip :
  746.                             retval.append(v)
  747.                     return retval
  748.                 else :
  749.                     return list(sets.Set(lst))
  750.         # Select may be a single query string, or an array/tuple thereof
  751.         selectionF = _variablesToArray(selection,"selection")
  752.  
  753.         if type(offset) is not types.IntType or offset < 0 :
  754.             raise SPARQLError("'offset' argument is invalid")
  755.  
  756.         if limit != None :
  757.             if type(limit) is not types.IntType or limit < 0 :
  758.                 raise SPARQLError("'offset' argument is invalid")
  759.  
  760.         if orderBy != None :
  761.             results = self._orderedSelect(selectionF,orderBy,orderAscend)
  762.         else :
  763.             if self.parent1 != None and self.parent2 != None :
  764.                 results = self.parent1.select(selectionF) + self.parent2.select(selectionF)
  765.             else :
  766.                 # remember: _processResult turns the expansion results (an array of dictionaries)
  767.                 # into an array of tuples in the right, original order
  768.                 results = _processResults(selectionF,self.top.returnResult(selectionF))
  769.         if distinct :
  770.             retval = _uniquefyList(results)
  771.         else :
  772.             retval = results
  773.  
  774.         if limit != None :
  775.             return retval[offset:limit+offset]
  776.         elif offset > 0 :
  777.             return retval[offset:]
  778.         else :
  779.             return retval
  780.  
  781.     def construct(self,pattern=None) :
  782.         """
  783.         Expand the subgraph based on the pattern or, if None, the
  784.         internal bindings.
  785.  
  786.         In the former case the binding is used to instantiate the
  787.         triplets in the patterns; in the latter, the original
  788.         statements are used as patterns.
  789.  
  790.         The result is a separate triple store containing the subgraph.
  791.  
  792.         @param pattern: a L{GraphPattern<rdflib.sparql.graphPattern.GraphPattern>} instance or None
  793.         @return: a new triple store
  794.         @rtype: L{sparqlGraph<rdflib.sparql.sparqlGraph>}
  795.         """
  796.         if self.parent1 != None and self.parent2 != None :
  797.             return self.parent1.construct(pattern) + self.parent2.construct(pattern)
  798.         else :
  799.             subgraph = SPARQLGraph()
  800.             self.top.expandSubgraph(subgraph,pattern)
  801.             return subgraph
  802.  
  803.     def ask(self) :
  804.         """
  805.         Whether a specific pattern has a solution or not.
  806.         @rtype: Boolean
  807.         """
  808.         return len(self.select('*')) != 0
  809.  
  810.     #########################################################################################################
  811.     # The methods below are not really part of SPARQL, or may be used to a form of DESCRIBE. However, that latter
  812.     # is still in a flux in the draft, so we leave it here, pending
  813.  
  814.     def clusterForward(self,selection) :
  815.         """
  816.         Forward clustering, using all the results of the query as
  817.         seeds (when appropriate). It is based on the usage of the
  818.         L{cluster forward<rdflib.sparql.sparqlGraph.clusterForward>}
  819.         method for triple store.
  820.  
  821.         @param selection: a selection to define the seeds for
  822.         clustering via the selection; the result of select used for
  823.         the clustering seed
  824.  
  825.         @return: a new triple store
  826.         @rtype: L{sparqlGraph<rdflib.sparql.sparqlGraph>}
  827.         """
  828.         if self.parent1 != None and self.parent2 != None :
  829.             return self.parent1.clusterForward(selection) + self.parent2.clusterForward(selection)
  830.         else :
  831.             clusterF = SPARQLGraph()
  832.             for r in reduce(lambda x,y: list(x) + list(y),self.select(selection),()) :
  833.                 try :
  834.                     check_subject(r)
  835.                     self.triples.clusterForward(r,clusterF)
  836.                 except :
  837.                     # no real problem, this is a literal, just forget about it
  838.                     continue
  839.             return clusterF
  840.  
  841.     def clusterBackward(self,selection) :
  842.         """
  843.         Backward clustering, using all the results of the query as
  844.         seeds (when appropriate). It is based on the usage of the
  845.         L{cluster backward<rdflib.sparql.sparqlGraph.clusterBackward>}
  846.         method for triple store.
  847.  
  848.         @param selection: a selection to define the seeds for
  849.         clustering via the selection; the result of select used for
  850.         the clustering seed
  851.  
  852.         @return: a new triple store
  853.         @rtype: L{sparqlGraph<rdflib.sparql.sparqlGraph>}
  854.         """
  855.         if self.parent1 != None and self.parent2 != None :
  856.             return self.parent1.clusterBackward(selection) + self.parent2.clusterBackward(selection)
  857.         else :
  858.             clusterB = SPARQLGraph()
  859.             # to be on the safe side, see if the query has been properly finished
  860.             for r in reduce(lambda x,y: list(x) + list(y),self.select(selection),()) :
  861.                 self.triples.clusterBackward(r,clusterB)
  862.             return clusterB
  863.  
  864.     def cluster(self,selection) :
  865.         """
  866.         Cluster: a combination of L{Query.clusterBackward} and
  867.         L{Query.clusterForward}.  @param selection: a selection to
  868.         define the seeds for clustering via the selection; the result
  869.         of select used for the clustering seed
  870.         """
  871.         return self.clusterBackward(selection) + self.clusterForward(selection)
  872.  
  873.     def describe(self,selection,forward=True,backward=True) :
  874.         """
  875.         The DESCRIBE Form in the SPARQL draft is still in state of
  876.         flux, so this is just a temporary method, in fact.  It may not
  877.         correspond to what the final version of describe will be (if
  878.         it stays in the draft at all, that is).  At present, it is
  879.         simply a wrapper around L{cluster}.
  880.  
  881.         @param selection: a selection to define the seeds for
  882.         clustering via the selection; the result of select used for
  883.         the clustering seed
  884.  
  885.         @param forward: cluster forward yes or no
  886.         @type forward: Boolean
  887.         @param backward: cluster backward yes or no
  888.         @type backward: Boolean
  889.         """
  890.         if forward and backward :
  891.             return self.cluster(selection)
  892.         elif forward :
  893.             return self.clusterForward(selection)
  894.         elif backward :
  895.             return self.clusterBackward(selection)
  896.         else :
  897.             return SPARQLGraph()
  898.  
  899.